package org.my.workflow.test;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import org.my.workflow.Payload;
import org.my.workflow.Workflow;
import org.my.workflow.WorkflowConfig;
import org.my.workflow.element.ActionElement;
import org.my.workflow.element.StepElement;

/**
 * @author <a href="mailto:yang_y@sysnet.com.cn">Young Yang</a>
 */
public class ExampleWorkflow {

    Workflow workflow = null;

    public ExampleWorkflow() throws Exception {
        WorkflowConfig workflowConfig = WorkflowConfig.loadConfiguration(new File("example.xml").toURI().toURL());
        workflow = workflowConfig.newWorkflow("user1",new Payload() {
            public Object get() {
                return "Hi, I'm payload!";
            }
            public String toSerializeString() {
                return get().toString();
            }
        });
    }

    public void autoTransition(){
        System.out.println();
        System.out.println("Starting next step...");
        List<StepElement> stepElements = workflow.getCurrentStepElements();
        System.out.print("Current Step Element are: ");
        for(StepElement stepElement : stepElements){
            System.out.print(stepElement.getId() + "[" + stepElement.getName() + "] ");
        }
        System.out.println();

        System.out.print("Current Action Element are: ");
        List<ActionElement> actionElements = workflow.getAvailableActionElements();
        for(ActionElement actionElement : actionElements){
            System.out.print(actionElement.getId() + "[" + actionElement.getName() + "] ");
        }
        System.out.println();

        for(ActionElement actionElement : actionElements){
            workflow.doAction(actionElement.getId());
        }
        System.out.println("Finish step!");
    }

    public String[] getCurrentStepId(){
        return workflow.getCurrentStepElementIds();
    }

    public static void main(String[] args) throws Exception {
        ExampleWorkflow exampleWorkflow = new ExampleWorkflow();
        exampleWorkflow.autoTransition();
        System.out.println(Arrays.toString(exampleWorkflow.getCurrentStepId()));
        exampleWorkflow.autoTransition();
        System.out.println(Arrays.toString(exampleWorkflow.getCurrentStepId()));
        exampleWorkflow.autoTransition();
        System.out.println(Arrays.toString(exampleWorkflow.getCurrentStepId()));
        exampleWorkflow.autoTransition();
        System.out.println(Arrays.toString(exampleWorkflow.getCurrentStepId()));
        exampleWorkflow.autoTransition();
        System.out.println(Arrays.toString(exampleWorkflow.getCurrentStepId()));

    }


}
